package org.jeecg.modules.jiucxldj.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.JsonUtils;
import org.jeecg.modules.jiucxldj.entity.AgJiucxldj;
import org.jeecg.modules.jiucxldj.entity.AgJiucxldjResource;
import org.jeecg.modules.jiucxldj.mapper.AgJiucxldjResourceMapper;
import org.jeecg.modules.jiucxldj.mapper.AgJiucxldjMapper;
import org.jeecg.modules.jiucxldj.service.IAgJiucxldjService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: 纠察巡逻登记
 * @Author: jeecg-boot
 * @Date:   2020-07-14
 * @Version: V1.0
 */
@Service
public class AgJiucxldjServiceImpl extends ServiceImpl<AgJiucxldjMapper, AgJiucxldj> implements IAgJiucxldjService, JavaDelegate {

	@Autowired
	private AgJiucxldjMapper agJiucxldjMapper;
	@Autowired
	private AgJiucxldjResourceMapper agJiucxldjResourceMapper;
	@Autowired
	private IdentityService identityservice;
	@Autowired
	private RuntimeService runtimeservice;
	@Autowired
	private TaskService taskService;
	@Autowired
	private RepositoryService repositoryService;

	@Override
	@Transactional
	public void saveMain(AgJiucxldj agJiucxldj, List<AgJiucxldjResource> agJiucxldjResourceList) {
		agJiucxldjMapper.insert(agJiucxldj);
		if(agJiucxldjResourceList!=null && agJiucxldjResourceList.size()>0) {
			for(AgJiucxldjResource entity:agJiucxldjResourceList) {
				//外键设置
				entity.setZhubId(agJiucxldj.getId());
				agJiucxldjResourceMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(AgJiucxldj agJiucxldj,List<AgJiucxldjResource> agJiucxldjResourceList) {
		agJiucxldjMapper.updateById(agJiucxldj);
		
		//1.先删除子表数据
		agJiucxldjResourceMapper.deleteByMainId(agJiucxldj.getId());
		
		//2.子表数据重新插入
		if(agJiucxldjResourceList!=null && agJiucxldjResourceList.size()>0) {
			for(AgJiucxldjResource entity:agJiucxldjResourceList) {
				//外键设置
				entity.setZhubId(agJiucxldj.getId());
				agJiucxldjResourceMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		agJiucxldjResourceMapper.deleteByMainId(id);
		agJiucxldjMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			agJiucxldjResourceMapper.deleteByMainId(id.toString());
			agJiucxldjMapper.deleteById(id);
		}
	}


	@Override
	public void startFlow(AgJiucxldj agLeave, LoginUser loginUser) {
//        int days = (int) ((agLeave.getLeaveEndtime().getTime() - agLeave.getLeaveStarttime().getTime()) / (1000*3600*24));
		String businesskey = "" + agLeave.getId();

		identityservice.setAuthenticatedUserId(loginUser.getUsername());

		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("applyUser", loginUser.getUsername());
		variables.put("auditUser",agLeave.getZhuanfaren());
//        variables.put("day",days);

		Model modelData = repositoryService.getModel("445008");
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(modelData.getDeploymentId()).singleResult();

		ProcessInstance instance=runtimeservice.startProcessInstanceById(processDefinition.getId(),businesskey,variables);
		String instanceid=instance.getId();
		agLeave.setProcessInstanceId(instanceid);
		Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).taskAssignee(loginUser.getUsername()).singleResult();

		agLeave.setTaskId(task.getId());

		this.baseMapper.updateById(agLeave);

		taskService.complete(task.getId(),variables);
	}

	@Override
	public void complateTask(LoginUser loginUser, String taskId, Integer status, String memo) {
//        taskService.claim(taskId,loginUser.getId());

		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		Map params = new HashMap(){{
			put("status", status);
			put("memo",memo);
		}};
		taskService.addComment(taskId,task.getProcessInstanceId(), JsonUtils.toJsonString(params));
		taskService.complete(taskId,params);
	}

	@Override
	public void applyAgin(AgJiucxldj agLeave) {
		agJiucxldjMapper.updateById(agLeave);

		Task task = null;//taskService.createTaskQuery().processInstanceId(agLeave.getProcessInstanceId()).taskAssignee(agLeave.getApplicant()).singleResult();

		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("day",0);
		variables.put("status",1);

		taskService.complete(task.getId(),variables);
	}

	@Override
	public IPage<AgJiucxldj> myApplyList(Page<AgJiucxldj> page, LoginUser user, AgJiucxldj agLeave) {
		return this.baseMapper.myApplyList(page, user, agLeave);
	}

	@Override
	public IPage<AgJiucxldj> myTodoTasks(Page<AgJiucxldj> page, LoginUser user, AgJiucxldj agLeave) {
		return this.baseMapper.myTodoTasks(page, user, agLeave);
	}

	@Override
	public IPage<AgJiucxldj> myDoneTasks(Page<AgJiucxldj> page, LoginUser user, AgJiucxldj agLeave) {
		return this.baseMapper.myDoneTasks(page, user, agLeave);
	}

	@Override
	public void applyAgain(AgJiucxldj agJiucxldj, LoginUser user) {
		Task task = taskService.createTaskQuery().processInstanceId(
				agJiucxldj.getProcessInstanceId()).taskAssignee(user.getUsername()).singleResult();

		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("status", null);
		variables.put("auditUser", agJiucxldj.getShangbr());
		taskService.complete(task.getId(),variables);
	}

	@Override
	public void execute(DelegateExecution execution) {

	}
}
